using System;
using Csla;
using Ent.AccesoDatos;
using System.Collections.Generic;
using System.Data;

namespace EntGuayasSLA.BL
{
    [Serializable]
    public class Ubicacion : BusinessBase<Ubicacion>
    {
        #region Business Methods

        public static readonly PropertyInfo<Guid> IdProperty = RegisterProperty<Guid>(c => c.Id);
        public Guid Id
        {
            get { return GetProperty(IdProperty); }
            set { SetProperty(IdProperty, value); }
        }
        public static readonly PropertyInfo<Guid> ExpedienteIdProperty = RegisterProperty<Guid>(c => c.ExpedienteId);
        public Guid ExpedienteId
        {
            get { return GetProperty(ExpedienteIdProperty); }
            set { SetProperty(ExpedienteIdProperty, value); }
        }

        public static readonly PropertyInfo<int> OrdenProperty = RegisterProperty<int>(c => c.Orden);
        public int  Orden
        {
            get { return GetProperty(OrdenProperty); }
            set { SetProperty(OrdenProperty, value); }
        }
        public static readonly PropertyInfo<string> LogitudProperty = RegisterProperty<string>(c => c.Longitud);
        public string Longitud
        {
            get { return GetProperty(LogitudProperty); }
            set { SetProperty(LogitudProperty, value); }
        }

        public static readonly PropertyInfo<string> LatidtudProperty = RegisterProperty<string>(c => c.Latitud);
        public string Latitud
        {
            get { return GetProperty(LatidtudProperty); }
            set { SetProperty(LatidtudProperty, value); }
        }

        public static readonly PropertyInfo<string> TipoProperty = RegisterProperty<string>(c => c.Tipo);
        public string Tipo
        {
            get { return GetProperty(TipoProperty); }
            set { SetProperty(TipoProperty, value); }
        }

        #endregion

        #region Business Rules

        protected override void AddBusinessRules()
        {
            // TODO: add validation rules
            base.AddBusinessRules();

            //BusinessRules.AddRule(new Rule(IdProperty));
        }

        private static void AddObjectAuthorizationRules()
        {
            // TODO: add authorization rules
            //BusinessRules.AddRule(...);
        }

        #endregion

        #region Factory Methods

        public static Ubicacion NewEditableRoot()
        {
            return DataPortal.Create<Ubicacion>();
        }

        public static Ubicacion GetEditableRoot(Guid id)
        {
            return DataPortal.Fetch<Ubicacion>(id);
        }

        public static void DeleteEditableRoot(Guid id)
        {
            DataPortal.Delete<Ubicacion>(id);
        }

        private Ubicacion()
        { /* Require use of factory methods */ }

        #endregion

        #region Data Access

        [RunLocal]
        protected override void DataPortal_Create()
        {
            // TODO: load default values
            // omit this override if you have no defaults to set
            base.DataPortal_Create();
        }

        private void DataPortal_Fetch(Guid criteria)
        {
            var accesoDatos = new DriveDataBase(TypeDataBase.PostgreSql);
            var parametros = new List<Parametro> { new Parametro("ec_exp_id", criteria.ToString()) };
            DataSet datos = accesoDatos.EjecutarDataSet("\"OBT_UBICACION\"", parametros);
            if (datos.Tables[0].Rows.Count > 0)
            {
                LoadProperty(IdProperty, Guid.Parse(datos.Tables[0].Rows[0]["ubc_id"].ToString()));
                LoadProperty(ExpedienteIdProperty, Guid.Parse(datos.Tables[0].Rows[0]["exp_id"].ToString()));
                LoadProperty(OrdenProperty, int.Parse( datos.Tables[0].Rows[0]["ubc_orden"].ToString()));
                LoadProperty(LogitudProperty,datos.Tables[0].Rows[0]["ubc_lontitud"].ToString());
                LoadProperty(LatidtudProperty,datos.Tables[0].Rows[0]["ubc_latitud"].ToString());
                LoadProperty(TipoProperty, datos.Tables[0].Rows[0]["ubc_tipo"].ToString());
            }
        }
       
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            var accesoDatos = new DriveDataBase(TypeDataBase.PostgreSql);
            var parametros = new List<Parametro> { 
                new Parametro("ec_ubc_id", ReadProperty(IdProperty).ToString()),
                new Parametro("ec_exp_id", ReadProperty(ExpedienteIdProperty)),
                new Parametro("ec_ubc_orden", ReadProperty(OrdenProperty)),
                new Parametro("ec_ubc_lontitud", ReadProperty(LogitudProperty)),
                new Parametro("ec_ubc_latitud", ReadProperty(LatidtudProperty)),
                new Parametro("ec_ubc_tipo", ReadProperty(TipoProperty))
            };
            accesoDatos.EjecutarQuery("\"INS_UBICACION\"", parametros);
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            var accesoDatos = new DriveDataBase(TypeDataBase.PostgreSql);
            var parametros = new List<Parametro> { 
                 new Parametro("ec_ubc_id", ReadProperty(IdProperty).ToString()),
                new Parametro("ec_exp_id", ReadProperty(ExpedienteIdProperty)),
                new Parametro("ec_ubc_orden", ReadProperty(OrdenProperty)),
                new Parametro("ec_ubc_lontitud", ReadProperty(LogitudProperty)),
                new Parametro("ec_ubc_latitud", ReadProperty(LatidtudProperty)),
                new Parametro("ec_ubc_tipo", ReadProperty(TipoProperty))
            };
            accesoDatos.EjecutarQuery("\"ACT_UBICACION\"", parametros);
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(this.ExpedienteId);
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        private void DataPortal_Delete(Guid criteria)
        {
            var accesoDatos = new DriveDataBase(TypeDataBase.PostgreSql);
            var parametros = new List<Parametro> { 
                new Parametro("ec_exp_id", criteria.ToString())
            };
            accesoDatos.EjecutarQuery("\"ELI_UBICACION\"", parametros);
        }

        #endregion
    }
}
